package com.spring.ioc.javaBean.beans;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyEditorSupport;
import java.util.stream.Stream;

public class BeanInfoDemo {
    public static void main(String[] args) throws IntrospectionException {
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class); // 提供一个停止类为了只找当前层次
//        Stream.of(beanInfo.getPropertyDescriptors()).forEach(System.out::println);
        Stream.of(beanInfo.getPropertyDescriptors()).forEach(propertyDescriptor -> {

            // PropertyDescriptor 允许添加属性编辑器 - PropertyEditor
            // GUI -> text(String) -> PropertyType
            // name -> String
            // age -> Integer
            Class<?> propertyType = propertyDescriptor.getPropertyType();
            String propertyName = propertyDescriptor.getName();
            if ("age".equals(propertyName)) { // 为“age”字段 / 属性增加 PropertyEditor
//                Integer.valueOf("")
                propertyDescriptor.setPropertyEditorClass(StringToIntegerPropertyEditor.class);
                propertyDescriptor.createPropertyEditor();
            }
        });
    }

    // 使用PropertyEditor -> PropertyEditorSupport实现
    static class StringToIntegerPropertyEditor extends PropertyEditorSupport {
        // Setter方法
        public void setAsText(String text) throws java.lang.IllegalArgumentException {
            Integer value = Integer.valueOf(text);
            setValue(value);
        }
    }
}
